package com.zcl.iweibo;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.zcl.bean.Task;
import com.zcl.bean.UserInfo;
import com.zcl.logic.MainService;
import com.zcl.logic.UserInfoService;
import com.zcl.utils.DialogFactory;

public class LoginActivity extends Activity implements IWeiboActivity {
	private final int DIALOG_LOADING = 1; // 加载进度条
	private final int DIALOG_USER_NULL = 2; // 获取本地用户数据为空
	private final int DIALOG_USER_LIST = 3; // 显示用户列表
	private Integer mCurrentUserID = -1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		init();
		
		Button btn = (Button)this.findViewById(R.id.btn_login);
		btn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) {
//				Task task = new Task();
//				task.setTaskId(Task.WEIBO_LOGIN);
//				MainService.newTask(task);
//				MainService.addActivity(LoginActivity.this);
				
				Intent intent = new Intent(LoginActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
			}
		});
		
		Button select = (Button) this.findViewById(R.id.btn_select_user);
		select.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_LOADING);
				Task task = new Task();
				task.setTaskId(Task.TAKEOUT_LOCAL_USERS);
				MainService.newTask(task);
				MainService.addActivity(LoginActivity.this);
			}
		});
		
		Button add = (Button)this.findViewById(R.id.btn_add);
		add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
			}
		});
	}
	
	
	@Override
	public void init() {
		Intent serviceIntent = new Intent(this, MainService.class);
		startService(serviceIntent);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void refresh(Object...objects) {
		MainService.removeActivity(LoginActivity.this);
		for(Object object : objects){
			Message msg = (Message)object;
			switch (msg.what) {
			case Task.TAKEOUT_LOCAL_USERS:{
				List<UserInfo> users = (List<UserInfo>)msg.obj;
				if (users == null || users.isEmpty()) {
					removeDialog(DIALOG_LOADING);
					showDialog(DIALOG_USER_NULL);
				}else {
					removeDialog(DIALOG_LOADING);
					UserInfo[] infos = (UserInfo[])users.toArray(new UserInfo[users.size()]);
					final Bundle bundle = new Bundle();
					bundle.putParcelableArray("infos", infos);
					showDialog(DIALOG_USER_LIST, bundle);
				}
			}
				break;
			case Task.WEIBO_LOGIN:{
				Intent intent = new Intent(this, AuthorizeActivity.class);
				startActivity(intent);
				this.finish();
			}	
				break;
			default:
				break;
			}
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		if (id == DIALOG_LOADING) {
			return DialogFactory.createProgressDialog(LoginActivity.this, getString(R.string.webview_loading_msg));
		} else if (id == DIALOG_USER_NULL) {
			return DialogFactory.createDialog(LoginActivity.this, R.string.text_info_tips, R.string.text_user_null, R.string.text_confirm, onClickListener);
		} else if (id == DIALOG_USER_LIST) {
			UserInfo[] infos = (UserInfo[]) args.getParcelableArray("infos");
			List<UserInfo> users = Arrays.asList(infos);
			return DialogFactory.createListDialog(LoginActivity.this, users, onItemClickListener);
		} else {
		}
		return super.onCreateDialog(id);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		ActivityManager activityMgr= (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
//		activityMgr.restartPackage(getPackageName());
		return super.onKeyDown(keyCode, event);
	}

	private OnClickListener onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			removeDialog(DIALOG_USER_NULL);
		}
	};
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			removeDialog(DIALOG_USER_LIST);
			ImageView imageView = (ImageView)view.findViewById(R.id.img_user_head);
			TextView textView = (TextView)view.findViewById(R.id.text_user_name);
			((ImageView)LoginActivity.this.findViewById(R.id.img_head)).setImageDrawable(imageView.getDrawable());
			((TextView)LoginActivity.this.findViewById(R.id.text_login_show_name)).setText(textView.getText());
		}
	};
}
